﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model.Sadharon;
using InventorySys.controller;
using Model.Upadan;
using Model.Sohaok;
using System.Globalization;
using System.Web.UI.WebControls;

namespace InventorySys.view
{
    public partial class Customer : Form
    {
        #region Private method
        private string _pageMode = "";
        private Model.Upadan.Customer customer;
        private Model.Upadan.Address address;
        private Model.Upadan.SaleType saleType;
        private Result result;
        private CustomerController customerController;
        private AddressController addressController;
        private SaleTypeController saleTypeController;
        private List<SaleType> allSaleType;
        #endregion

        #region constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public Customer()
        {
            InitializeComponent();
            // initiate customer controller
            customerController = new CustomerController();
            saleTypeController = new SaleTypeController();
            // load sale type
            loadSaleType();
            // load country
            loadCountryList();
        }
        #endregion

        #region load sale type
        /// <summary>
        /// load sale type
        /// </summary>
        private void loadSaleType()
        {
            allSaleType = (List<SaleType>)saleTypeController.GetAllSaleType().ResultObj;
            if (allSaleType != null)
            {
                foreach (SaleType _saleType in allSaleType)
                {
                    TextType.Items.Add(new Item(_saleType.Name, _saleType.Id));
                }
            }
        }
        #endregion

        #region load country list
        /// <summary>
        /// load country list
        /// </summary>
        private void loadCountryList()
        {
            try
            {
                CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
                SortedDictionary<string, string> regionLookup = new SortedDictionary<string, string>(StringComparer.Ordinal);
                foreach (CultureInfo cultInfo in cultures)
                {
                    RegionInfo regionInfo = new RegionInfo(cultInfo.LCID);
                    if (!regionLookup.ContainsKey(regionInfo.EnglishName))
                    {
                        regionLookup.Add(regionInfo.EnglishName, regionInfo.TwoLetterISORegionName);
                        TextCountry.Items.Add(new ListItem(regionInfo.EnglishName, regionInfo.TwoLetterISORegionName));
                    }
                }
            }
            catch (Exception e) { }
        }
        #endregion

        #region save customer details
        /// <summary>
        /// save customer details
        /// </summary>
        /// <returns></returns>
        private string saveCustomerDetails()
        {
            customer = collectCustomerData();
            address = collectAddressData();                        


            result = customerController.addCustomerDetails(customer, address);
            customer.Id = "";
            if (result.IsSuccess)
            {
                customer.Id = result.ResultObj.ToString();
            }
            MessageBox.Show(result.Message);
            _pageMode = "viewMode";
            return customer.Id;
        }
        #endregion

        #region update customer details
        /// <summary>
        /// update customer details
        /// </summary>
        /// <returns></returns>
        private string updateCustomerDetails()
        {
            customer = collectCustomerData();
            address = collectAddressData();

            result = customerController.updateCustomerDetails(customer, address);
            MessageBox.Show(result.Message);
            _pageMode = "viewMode";
            return customer.Id;
        }
        #endregion

        #region load cutomer details into page element
        /// <summary>
        /// load customer details in view & edit element
        /// </summary>
        /// <param name="customerId"></param>
        private void loadCustomerDetails(string customerId)
        {
            result = customerController.GetCustomerDetails(customerId);
            addressController = new AddressController();
            if (result.IsSuccess)
            {
                customer = (Model.Upadan.Customer)result.ResultObj;

                // header information
                label_ID.Text = customer.Id;
                label_CNAME.Text = customer.CompanyName;

                // view element
                LabelAccountReference.Text = customer.AccountReference;
                LabelCompanyName.Text = customer.CompanyName;
                LabelContactPerson.Text = customer.ContactPerson;
                // get sale type
                result = saleTypeController.GetSaleTypeDetails(customer.SaleTypeId);
                if (result.IsSuccess)
                {
                    saleType = (SaleType)result.ResultObj;
                    LabelType.Text = saleType.Name;
                    TextType.Text = saleType.Name;
                }
                // edit element
                TextAccountReference.Text = customer.AccountReference;
                TextCompanyName.Text = customer.CompanyName;
                TextContactPerson.Text = customer.ContactPerson;
                
                // get address details by supplier id
                result = null;
                result = addressController.GetAddressDetails(customer.Id);
                if (result.IsSuccess)
                {
                    address = (Address)result.ResultObj;

                    // view element
                    LabelAddress.Text = address.Description;
                    LabelEmail.Text = address.Email;
                    LabelFax.Text = address.Fax;
                    LabelMobile.Text = address.Mobile;
                    LabelPostCode.Text = address.PostCode;
                    LabelTelephone.Text = address.Telephone;
                    LabelWeb.Text = address.Web;
                    LabelCountry.Text = address.Country;

                    // edit element
                    TextAddress.Text = address.Description;
                    TextEmail.Text = address.Email;
                    TextFax.Text = address.Fax;
                    TextMobile.Text = address.Mobile;
                    TextPostCode.Text = address.PostCode;
                    TextTelephone.Text = address.Telephone;
                    TextWeb.Text = address.Web;
                    TextCountry.Text = address.Country;

                }
            }

        }
        #endregion

        #region collect customer information
        /// <summary>
        /// collect customer information
        /// </summary>
        /// <returns></returns>
        private Model.Upadan.Customer collectCustomerData()
        {
            customer = new Model.Upadan.Customer();
            customer.AccountReference = TextAccountReference.Text;
            customer.CompanyName = TextCompanyName.Text;
            customer.ContactPerson = TextContactPerson.Text;
            Item item = (Item)TextType.SelectedItem;
            customer.SaleTypeId = item.getValue();
            customer.Live = "L";
            customer.Id = label_ID.Text;
            return customer;
        }
        #endregion

        #region collect address information
        /// <summary>
        /// collect address information
        /// </summary>
        /// <returns></returns>
        private Model.Upadan.Address collectAddressData()
        {
            address = new Model.Upadan.Address();
            address.Country = TextCountry.Text;
            address.Email = TextEmail.Text;
            address.Fax = TextFax.Text;
            address.Mobile = TextMobile.Text;
            address.PostCode = TextPostCode.Text;
            address.Telephone = TextTelephone.Text;
            address.Web = TextWeb.Text;
            address.Description = TextAddress.Text;
            address.PersonId = label_ID.Text;
            return address;
        }
        #endregion


        // dependencies
        #region customer details view mode
        /// <summary>
        /// customer details view mode
        /// </summary>
        /// <param name="id"></param>
        private void customerDetailsViewMode(string id)
        {
            loadCustomerDetails(id);
            hideEditMode();
            showViewMode();
        }
        #endregion

        #region customer details edit mode
        /// <summary>
        /// customer details edit mode
        /// </summary>
        /// <param name="id"></param>
        private void customerDetailsEditMode(string id)
        {
            loadCustomerDetails(id);
            showEditMode();
            hideViewMode();
        }
        #endregion

        #region decide page mode
        /// <summary>
        /// decide page mode
        /// </summary>
        /// <param name="pageMode"></param>
        /// <param name="_id"></param>
        public void decidePageMode(string pageMode, string _id)
        {
            _pageMode = pageMode;
            if (_pageMode.Equals("addMode"))
            {
                hideViewMode();
                ButtonCancel.Enabled = false;
            }
            else if (_pageMode.Equals("viewMode"))
            {
                customerDetailsViewMode(_id);
            }
        }
        #endregion

        #region hide view mode
        /// <summary>
        /// hide view mode
        /// </summary>
        private void hideViewMode()
        {
            LabelAccountReference.Hide();
            LabelAddress.Hide();
            LabelCompanyName.Hide();
            LabelContactPerson.Hide();
            LabelCountry.Hide();
            LabelEmail.Hide();
            LabelFax.Hide();
            LabelMobile.Hide();
            LabelPostCode.Hide();
            LabelTelephone.Hide();
            LabelType.Hide();
            LabelWeb.Hide();

            // hide related button
            ButtonEdit.Hide();
            ButtonPrint.Hide();            
        }
        #endregion

        #region show view mode
        /// <summary>
        /// show view mode
        /// </summary>
        private void showViewMode()
        {
            LabelAccountReference.Show();
            LabelAddress.Show();
            LabelCompanyName.Show();
            LabelContactPerson.Show();
            LabelCountry.Show();
            LabelEmail.Show();
            LabelFax.Show();
            LabelMobile.Show();
            LabelPostCode.Show();
            LabelTelephone.Show();
            LabelType.Show();
            LabelWeb.Show();
        }
        #endregion

        #region hide edit mode
        /// <summary>
        /// hide edit mode
        /// </summary>
        private void hideEditMode()
        {
            TextAccountReference.Hide();
            TextAddress.Hide();
            TextCompanyName.Hide();
            TextContactPerson.Hide();
            TextCountry.Hide();
            TextEmail.Hide();
            TextFax.Hide();
            TextMobile.Hide();
            TextPostCode.Hide();
            TextTelephone.Hide();
            TextType.Hide();
            TextWeb.Hide();
            
            // hide related button
            ButtonSave.Hide();
            ButtonCancel.Hide();
            // show related button
            ButtonEdit.Show();
            ButtonPrint.Show();

        }
        #endregion

        #region show edit mode
        /// <summary>
        /// show edit mode
        /// </summary>
        private void showEditMode()
        {
            TextAccountReference.Show();
            TextAddress.Show();
            TextCompanyName.Show();
            TextContactPerson.Show();
            TextCountry.Show();
            TextEmail.Show();
            TextFax.Show();
            TextMobile.Show();
            TextPostCode.Show();
            TextTelephone.Show();
            TextType.Show();
            TextWeb.Show();

            // show related button
            ButtonSave.Show();
            ButtonCancel.Show();
            ButtonCancel.Enabled = true;
            // hide related button
            ButtonEdit.Hide();
            ButtonPrint.Hide();
        }
        #endregion

        // Button section
        #region close button
        /// <summary>
        /// close button handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region save button
        /// <summary>
        /// save button handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonSave_Click(object sender, EventArgs e)
        {
            string customerId = "";
            if (_pageMode.Equals("addMode"))
            {
                if (isCustomerOK())
                {
                    customerId = saveCustomerDetails();
                    if (customerId != null || customerId != "")
                    {
                        customerDetailsViewMode(customerId);
                    }
                }
            }

            else if (_pageMode.Equals("viewMode"))
            {
                if (isCustomerOK())
                {
                    customerId = updateCustomerDetails();
                    if (customerId != null || customerId != "")
                    {
                        customerDetailsViewMode(customerId);
                    }
                }
            }
        }
        #endregion

        #region edit button
        /// <summary>
        /// edit button habdler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonEdit_Click(object sender, EventArgs e)
        {
            customerDetailsEditMode(label_ID.Text.Trim());
        }
        #endregion

        #region cancel button
        /// <summary>
        /// cancel button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            customerDetailsViewMode(label_ID.Text.Trim());
        }
        #endregion


        // validation section
        #region validation section
        /// <summary>
        /// validation of required element
        /// </summary>
        /// <returns></returns>
        private bool isCustomerOK()
        {
            bool isOK = true;
            customerErrorProvider.Clear();

            if (TextAccountReference.Text == string.Empty)
            {
                customerErrorProvider.SetError(TextAccountReference, "Please enter account reference");
                isOK = false;
            }
            if (TextType.Text == string.Empty)
            {
                customerErrorProvider.SetError(TextType, "Please select sale type");
                isOK = false;
            }
            if (TextCompanyName.Text == string.Empty)
            {
                customerErrorProvider.SetError(TextCompanyName, "Please enter company person");
                isOK = false;
            }
            if (TextContactPerson.Text == string.Empty)
            {
                customerErrorProvider.SetError(TextContactPerson, "Please enter contact person");
                isOK = false;
            }
            if (TextCountry.Text == string.Empty)
            {
                customerErrorProvider.SetError(TextCountry, "Please select country");
                isOK = false;
            }
            if (TextAddress.Text == string.Empty)
            {
                customerErrorProvider.SetError(TextAddress, "Please enter address");
                isOK = false;
            }
            if (TextMobile.Text == string.Empty)
            {
                customerErrorProvider.SetError(TextMobile, "Please enter mobile no");
                isOK = false;
            }



            return isOK;
        }
        #endregion

    }
}
